using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BanZuiCloudControlAgent.Utils.Common;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// MQTT主题常量类，用于集中管理所有MQTT主题，避免硬编码
    /// </summary>
    public class MqttTopics
    {
        private readonly string _topicPrefix;
        private readonly string _deviceCode;

        /// <summary>
        /// 使用指定的主题前缀和设备码初始化MqttTopics类的新实例
        /// </summary>
        /// <param name="topicPrefix">主题前缀，默认为"cloud"</param>
        /// <param name="deviceCode">设备码</param>
        public MqttTopics(string topicPrefix, string deviceCode)
        {
            _topicPrefix = string.IsNullOrEmpty(topicPrefix) ? "cloud" : topicPrefix;
            _deviceCode = deviceCode;
        }

        /// <summary>
        /// 获取设备状态上报主题
        /// </summary>
        public string StatusReport => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.STATUS_REPORT, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取任务下发主题
        /// </summary>
        public string TaskDispatch => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.TASK_DISPATCH, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取任务上报主题
        /// </summary>
        public string TaskReport => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.TASK_REPORT, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取配置下发主题
        /// </summary>
        public string Config => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.CONFIG, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取错误上报主题
        /// </summary>
        public string ErrorReport => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.ERROR_REPORT, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取QQ在线列表上报主题
        /// </summary>
        public string QQOnlineReport => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.QQ_ONLINE_REPORT, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取采集上报主题
        /// </summary>
        public string CollectReport => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.COLLECT_REPORT, _topicPrefix, _deviceCode);
        
        /// <summary>
        /// 获取更新任务主题
        /// </summary>
        public string UpdateTask => MqttTopicTemplates.BuildTopic(MqttTopicTemplates.UPDATE_TASK, _topicPrefix, _deviceCode);

        /// <summary>
        /// 获取遗嘱消息主题（与状态上报主题相同）
        /// </summary>
        public string WillMessage => StatusReport;

        /// <summary>
        /// 获取设备需要订阅的所有主题列表
        /// </summary>
        /// <returns>设备需要订阅的主题列表</returns>
        public List<string> GetSubscribeTopics()
        {
            return new List<string>
            {
                TaskDispatch,
                Config,
                UpdateTask
            };
        }

        /// <summary>
        /// 根据消息类型获取对应的发布主题
        /// </summary>
        /// <param name="messageType">消息类型</param>
        /// <returns>对应的发布主题</returns>
        public string GetPublishTopicByMessageType(string messageType)
        {
            switch (messageType)
            {
                case MessageTypeConstants.STATUS_REPORT:
                    return StatusReport;
                case MessageTypeConstants.TASK_REPORT:
                    return TaskReport;
                case MessageTypeConstants.ERROR_REPORT:
                    return ErrorReport;
                case MessageTypeConstants.QQ_ONLINE_REPORT:
                    return QQOnlineReport;
                case MessageTypeConstants.COLLECT_REPORT:
                    return CollectReport;
                case MessageTypeConstants.DISCONNECT:
                    return StatusReport;
                default:
                    throw new ArgumentException($"不支持的消息类型: {messageType}", nameof(messageType));
            }
        }
    }
} 